﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BlogManager.Application.Controls
{
    public partial class Category : System.Web.UI.UserControl
    {
        private int? SiteId = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["SiteId"]))
                SiteId = Convert.ToInt32(Request.QueryString["SiteId"]);

            if(!IsPostBack)
                loadCategories();
        }

        protected void ImageButtonForAddigCategories_Click(object sender, EventArgs e)
        {
            var category = new BlogManager.Data.Entities.Blog_Category();
            category.SiteId = SiteId.Value;
            category.Name = TextBoxForCategoryName.Text;
            category.Save();

            ReBind();

            //Finally clear the textbox...
            TextBoxForCategoryName.Text = "";
        }

        protected void ReBind()
        {
            CheckBoxListForSelectingCategories.Items.Clear();
            loadCategories();

        }

        protected void loadCategories()
        {
            var categories = BlogManager.Data.Entities.Blog_Category.All().Where(x => x.SiteId == SiteId).OrderBy(x=>x.Name);
            CheckBoxListForSelectingCategories.DataSource = categories;
            CheckBoxListForSelectingCategories.DataValueField = Data.Entities.Blog_CategoriesTable.IdColumn;
            CheckBoxListForSelectingCategories.DataTextField = BlogManager.Data.Entities.Blog_CategoriesTable.NameColumn;
            CheckBoxListForSelectingCategories.DataBind();

            setSelected();
        }

        protected void setSelected()
        {
            if (_selectedCategories.Count > 0)
                foreach (var category in _selectedCategories)
                    for (var i = 0; i < CheckBoxListForSelectingCategories.Items.Count; i++)
                        if (CheckBoxListForSelectingCategories.Items[i].Value == category.ToString())
                        {
                            CheckBoxListForSelectingCategories.Items[i].Selected = true;
                            break;
                        }
        }

        private List<int> _selectedCategories = new List<int>();

        public List<int> SelectedCategories
        {
            get
            {
                var res = new List<int>();

                for (var i = 0; i < CheckBoxListForSelectingCategories.Items.Count; i++)
                {
                    if (CheckBoxListForSelectingCategories.Items[i].Selected)
                        res.Add(Convert.ToInt32(CheckBoxListForSelectingCategories.Items[i].Value));
                }

                return res;
            }

            set
            {
                _selectedCategories = value;
                

            }
        }


    }
}